package com.me.jingdongshop.ui.activity;

import android.content.Intent;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.BaseActivity;
import com.me.common.base.Const;
import com.me.common.base.Global;
import com.me.common.util.SharedPreUtil;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.LocationList;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.TestFragmentPre;
import com.me.jingdongshop.ui.adapter.AddressListAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by ldl on 2017/5/7.
 * 地址列表界面
 */

public class AddressListActivity extends BaseActivity {
    public static final int REQUEST_CODE = 0;
    public static final int RESULT_CODE = 01;
    private TextView tvAddresslistBack;     // 地址列表返回
    private TextView tvAddresslistAdd;      // 新增列表
    private android.support.v7.widget.RecyclerView lvAddressList;
    @Inject
    TestFragmentPre pre;
    private List<LocationList.AddressListBean> mAddressList = new ArrayList<>();
    private AddressListAdapter mAdapter;
    private int currentId = -1;
    private android.widget.Button lvAddressYes;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_shopcart_addresslist;
    }

    @Override
    public void initView() {
        tvAddresslistBack = (TextView) findViewById(R.id.tv_addresslist_back);
        tvAddresslistAdd = (TextView) findViewById(R.id.tv_addresslist_add);
        lvAddressList = (RecyclerView) findViewById(R.id.lv_address_list);
        lvAddressYes = (Button) findViewById(R.id.lv_address_yes);

        lvAddressList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AddressListAdapter(this, mAddressList);
        lvAddressList.setAdapter(mAdapter);
    }

    @Override
    public void initListener() {
        tvAddresslistBack.setOnClickListener(this);
        tvAddresslistAdd.setOnClickListener(this);
        lvAddressYes.setOnClickListener(this);
    }

    @Override
    public void initData() {
        pre = new TestFragmentPre(this);
        initAddress();
    }

    @Override
    public void onClick(View v, int id) {
        switch (id) {
            case R.id.tv_addresslist_back:
                finish();
                break;
            case R.id.tv_addresslist_add:
                startActivityForResult(new Intent(this, AddressManageActivity.class), REQUEST_CODE);
                break;
            case R.id.lv_address_yes:
                selectAddress();
                break;
        }
    }

    private void selectAddress() {
        if (currentId == -1) {
            Toast.makeText(this, "请选择/新增地址", Toast.LENGTH_SHORT).show();
            return;
        }
        for (LocationList.AddressListBean bean :
                mAddressList) {
            if (currentId == bean.getId()) {

                Intent data = new Intent();
                data.putExtra("address", bean);
                setResult(RESULT_CODE, data);
                finish();
                return;
            }
        }
    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType == IHttpService.HTTP_ADDRESS_LIST) {
            mAddressList.clear();
            LocationList locationList = (LocationList) msg.obj;
            mAddressList = locationList.getAddressList();
            mAdapter.setDatas(mAddressList);
        }
        if (reqType == IHttpService.HTTP_ADDRESS_DEF) {
            Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
            initAddress();
        }
    }

    @Override
    public void onHttpError(int reqType, String error) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == AddressManageActivity.RESULT_CODE) {
            initAddress();
        }
    }

    private void initAddress() {
        String uid = SharedPreUtil.getString(Global.mContext, Const.KEY_UID, "");
        if (!TextUtils.isEmpty(uid)) {
            pre.getAddressList(uid);
        }
    }

    public void setDefault(int id) {
        if (id == currentId) {
            return;
        }
        String uid = SharedPreUtil.getString(Global.mContext, Const.KEY_UID, "");
        this.currentId = id;
        if (!TextUtils.isEmpty(uid)) {
            pre.getAddressDefault(uid, id);
        }
    }

    public void setCurrentId(int id) {
        this.currentId = id;
    }
}
